<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row >
          <el-col :span="18">
            <el-form ref="form"  :model="form" label-width="80px" :inline="true">
              <el-form-item label="学科名称">
                <el-input size="small" class="el-input" v-model="form.subjectName"></el-input>
              </el-form-item>
              <el-form-item>
                  <el-button size="small" @click="clear">清除</el-button>
                  <el-button size="small" type="primary" @click="search">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" style="text-align:right">
            <el-button  icon="el-icon-edit" size="small" type="success" @click="addSubject">新增学科</el-button>
          </el-col>
        </el-row>
        <!-- alert 组件  -->
        <el-alert
          :closable="false"
          :title =title
          type="info"
          show-icon>
        </el-alert>

        <!-- 表格组件 -->
        <el-table
          class="el-table-list"
          :data="list"
          style="width: 100%">
          <el-table-column type="index" label="序号" />
          <el-table-column
            prop="subjectName"
            label="学科名称"
            width="160">
          </el-table-column>
          <el-table-column
            prop="username"
            label="创建者"
            width="160">
          </el-table-column>
          <el-table-column
            prop="addDate"
            label="创建日期">
            <template slot-scope="scope">
              {{scope.row.addDate | formatTime}}
            </template>
          </el-table-column>
          <el-table-column
            prop="isFrontDisplay"
            label="前台是否显示">
            <template slot-scope="scope">
              {{scope.row.isFrontDisplay ? '是' : '否'}}
            </template>
          </el-table-column>
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录">
          </el-table-column>
          <el-table-column
            prop="isFrontDisplay"
            label="标签">
          </el-table-column>
          <el-table-column
            prop="totals"
            label="题目数量">
          </el-table-column>
          <el-table-column
            label="操作"
             width="280">
            <template slot-scope="scope">
          <el-button type="text" @click="gotoDirectorys(scope.row)">学科分类</el-button>
          <el-button type="text" @click="gotoTags(scope.row)">学科标签</el-button>
          <el-button type="text" @click="changeSubject(scope.row)">修改</el-button>
          <el-button type="text" @click="delSubject(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <!-- handleSizeChange 每页显示的条数对应事件 -->
        <!-- handleCurrentChange 显示当前页 对应事件-->
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.page"
          :page-sizes="[10, 15, 20, 25]"
          :page-size="query.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total">
        </el-pagination>

        <!-- 新增学科弹框组件 -->
        <el-dialog
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :title="isChange? '新增学科' : '修改学科'"
          v-if="dialogVisible"
          visible
          width="25%"
          :before-close="handleClose">
          <el-form v-if="dialogVisible" ref="inputform" :rules="rules" :model="addList" label-width="80px">
            <el-form-item label="学科名称"  prop="subjectName">
                  <el-input v-model="addList.subjectName" placeholder="请输入学科名称" ></el-input>
            </el-form-item>
            <el-form-item label="是否显示" prop="isFrontDisplay">
                    <el-switch
                      v-model="addList.isFrontDisplay"
                       active-color="#13ce66"
                      inactive-color="#ff4949"
                    />
            </el-form-item>
            <el-form-item class="btn">
                  <el-button @click="cancel">取 消</el-button>
                  <el-button type="primary" @click="chabgeState">确 定</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>

      </el-card>
    </div>
  </div>
</template>

<script>
// 导入学科列表
import { list, add, detail, update, remove } from '@/api/hmmm/subjects.js'
import dayjs from 'dayjs'
export default {
  name: 'Subject',
  data () {
    return {
      dialogVisible: false, // 弹框默认隐藏
      query: {
        subjectName: '',
        page: 1, // 当前页
        pagesize: 10 // 每页显示条数
      },
      total: 0, // 总条数
      list: [], // 从接口处取回
      addList: {
        subjectName: '',
        isFrontDisplay: true
      },
      form: {
        name: ''
      },
      // 校验规则
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      isChange: false // 是否是修改状态
    }
  },
  filters: {
    formatTime (time) {
      return dayjs(time).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  // 计算属性
  computed: {
    title () {
      return `数据一共 ${this.total} 条`
    }
  },
  created () {
    // 调用获取全部学科列表的方法
    this.showList()
  },

  methods: {
    // 编程式导航：路由跳转\
    // 跳转到学科分类
    gotoDirectorys (row) {
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },

    // 跳转到学科标签
    gotoTags (row) {
      this.$router.push({
        path: '/subjects/tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },

    // 点击取消时触发的事件
    cancel () {
      this.dialogVisible = false
      // 清空表单
      this.addList = {
        subjectName: '',
        isFrontDisplay: ''
      }
    },
    // 删除学科
    async delSubject (row) {
      const confirmRes = await this.$confirm('此操作将永久删除学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('您取消了删除操作！')
      await remove(row)
      // 只有一条数据，点击删除以后
      if (this.list.length === 1) {
        // 页码 -1
        this.query.page--
        if (this.query.page < 1) {
          this.query.page = 1
        }
      }

      this.$message.success('删除学科成功！')
      this.showList()
    },

    // 修改学科
    async changeSubject (row) {
      this.dialogVisible = true
      this.isChange = false
      // 调用接口获取数据
      const res = await detail(row)
      // 进行数据回填
      this.addList.subjectName = res.data.subjectName
      if (res.data.isFrontDisplay) {
        this.addList.isFrontDisplay = true
      } else {
        this.addList.isFrontDisplay = false
      }
      this.addList.id = row.id
    },

    // 添加学科
    async addSubject () {
      this.dialogVisible = true
      // 设置为添加状态
      this.isChange = true
    },

    // 点击按钮清除学科名称
    clear () {
      this.form.subjectName = ''
      this.showList()
    },

    // 点击按钮搜索学科名称
    search () {
      this.query.page = 1
      this.showList()
    },

    // 获取学科列表
    async showList () {
      this.query.subjectName = this.form.subjectName
      const res = await list(this.query)
      // 数据回填
      this.list = res.data.items
      this.total = res.data.counts
    },

    // 每页条数发生改变时触发的事件
    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      this.showList()
    },

    // 页码发生改变时触发的事件
    handleCurrentChange (newPage) {
      this.query.page = newPage
      this.showList()
    },

    // 关闭弹框
    handleClose (done) {
      this.dialogVisible = false
      this.addList = {
        subjectName: '',
        isFrontDisplay: ''
      }
      done()
    },

    chabgeState () {
    // 兜底验证
      this.$refs.inputform.validate(async valid => {
        if (!valid) return
        if (this.isChange) {
          const res = await add(this.addList)
          console.log(res)
          this.showList()
          this.$message.success('新增学科成功！')
          this.dialogVisible = false
        } else {
          await update(this.addList)
          this.showList()
          this.$message.success('修改学科成功！')
          this.dialogVisible = false
        }

        this.addList = {
          subjectName: '',
          isFrontDisplay: false
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px;
  height: 32px;
}
.el-table-list {
  margin-top: 15px;
}
.el-pagination {
  margin-top: 15px;
  text-align: right;
}
.el-input--medium {
    font-size: 14px;
    width: 100%;
}
.btn {
  margin-bottom: 10px;
  text-align: right;
}
</style>
